﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using GeneralLibs;

namespace Lists
{
    public class DichVu : DoiTuong
    {
        #region string Ma property
        public string Ma
        {
            get
            {
                if (_DataRow["Ma"] is string) return (string)_DataRow["Ma"];
                return "";
            }
            set
            {
                _DataRow["Ma"] = value;
            }
        }
        #endregion

        #region string Ten property
        public string Ten
        {
            get
            {
                if (_DataRow["Ten"] is string) return (string)_DataRow["Ten"];
                return "";
            }
            set
            {
                _DataRow["Ten"] = value;
            }
        }
        #endregion

        #region string DonViTinh property
        public string DonViTinh
        {
            get
            {
                if (_DataRow["DonViTinh"] is string) return (string)_DataRow["DonViTinh"];
                return "";
            }
            set
            {
                _DataRow["DonViTinh"] = value;
            }
        }
        #endregion

        #region string MoTa property
        public string MoTa
        {
            get
            {
                if (_DataRow["MoTa"] is string) return (string)_DataRow["MoTa"];
                return "";
            }
            set
            {
                _DataRow["MoTa"] = value;
            }
        }
        #endregion

        #region decimal GiaDichVu property
        public decimal GiaDichVu
        {
            get
            {
                if (_DataRow["GiaDichVu"] is decimal) return (decimal)_DataRow["GiaDichVu"];
                return 0;
            }
            set
            {
                _DataRow["GiaDichVu"] = value;
            }
        }
        #endregion

        #region decimal GiaBHYT property
        public decimal GiaBHYT
        {
            get
            {
                if (_DataRow["GiaBHYT"] is decimal) return (decimal)_DataRow["GiaBHYT"];
                return 0;
            }
            set
            {
                _DataRow["GiaBHYT"] = value;
            }
        }
        #endregion

        #region string KetLuanMacDinh property
        public string KetLuanMacDinh
        {
            get
            {
                if (_DataRow["KetLuanMacDinh"] is string) return (string)_DataRow["KetLuanMacDinh"];
                return "";
            }
            set
            {
                _DataRow["KetLuanMacDinh"] = value;
            }
        }
        #endregion

        #region Guid NhomDichVuID property
        public Guid NhomDichVuID
        {
            get
            {
                if (_DataRow["NhomDichVuID"] is Guid) return (Guid)_DataRow["NhomDichVuID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["NhomDichVuID"] = value;
            }
        }
        #endregion

        public DichVu(DataRow dataRow)
            : base(dataRow)
        {
        }

        public DanhMucVatTuTieuHao _DanhSachVatTuTieuHao = new DanhMucVatTuTieuHao();
        public DanhMucThongSoCuaDichVu _DanhSachThongSoDichVu = new DanhMucThongSoCuaDichVu();

        public void LoaKhoanMucdVatTuTieuHao()
        {
            try
            {
                if (this == null) this.ID = Guid.Empty;
                _DanhSachVatTuTieuHao.LoadFromDB(this.ID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public void LoaKhoanMucThongSoCuaDichVu()
        {
            try
            {
                if (this == null) this.ID = Guid.Empty;
                _DanhSachThongSoDichVu.LoadFromDB(this.ID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }

    public class DanhMucDichVu : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucDichVu _Instance;
        public static DanhMucDichVu Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucDichVu();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    try
                    {
                        UniqueConstraint uniqueConstraint = new UniqueConstraint(_Instance.DataTable.Columns["Ma"]);
                        _Instance.DataTable.Constraints.Add(uniqueConstraint);
                        _Instance.DataView.Sort = "Ma";
                    }
                    catch (Exception ex)
                    {
                    }
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "Clinic_DichVu";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new DichVu((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public DichVu this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new DichVu(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public DichVu NewDoiTuong()
        {
            try
            {
                return new DichVu(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DichVu GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new DichVu(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DichVu GetByID(Guid objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        public DichVu GetByMa(string ma)
        {
            try
            {
                return GetByProperty("Ma", ma);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        public DichVu GetByTen(string ten)
        {
            try
            {
                return GetByProperty("Ten", ten);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DichVu GetByMaOrTen(string text)
        {
            try
            {
                DichVu dv = null;
                dv = GetByProperty("Ma", text);
                if(dv == null)
                    dv = GetByProperty("Ten", text);
                return dv; //Nếu đến đây mà cả mã cả tên đều k tìm thấy thì = null
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucDichVu GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DanhMucDichVu Filter(string filterExpression)
        {
            try
            {
                DanhMucDichVu obj = new DanhMucDichVu();
                obj.DataView = new DataView(DanhMucDichVu.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
